﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTAB45DOC : DocumentBase
    {
        #region-------------Properties-----------------------
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        JLTAB45DOCData m_Data;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        #region=====vwABPOS=======
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTBABST=======
        CollectionViewSource vwTBABST
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTBABST");
            }
        }
        DataRow dwTBABST
        {
            get
            {
                if (vwTBABST.View == null)
                    return null;
                if ((DataRowView)vwTBABST.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTBABST.View.CurrentItem).Row;
            }
        }
        #endregion
        string ART
        {
            get
            {
                return m_Data.Right("  " + txtArt.Text.ToString().Trim(), 2);
            }
        }
        string KRS
        {
            get
            {
                return m_Data.Right("  " + txtFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtFKRS.Text = value;
            }
        }
        string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtFLFD.Text = value;
            }
        }
        string POS
        {
            get
            {
                return m_Data.Right("000" + txtFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtFPOS.Text = value;
            }
        }
        BindingListCollectionView cvTBABST
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgTBABST.ItemsSource);
            }
        }
        ITXEXRPT ITXEXRPT;
        #endregion
        public JLTAB45DOC()
        {
            InitializeComponent();
            this.Name = "浆纱生产";
            m_Data = new JLTAB45DOCData();

            KRS = "JS ";
            LFD = "";
            POS = "1";
            txtBCode.Text = "";
            if (app.User.Trim() == "JLTV")
            {
                txtBCode.Text = "SZ000143";
            }
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";
            vwTBABST.View.CurrentChanged += View_CurrentChanged;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtFLFD.Focus();
        }
        void View_CurrentChanged(object sender, EventArgs e)
        {
            if (dwTBABST == null)
                return;
            txtKART.Text = dwTBABST["BMKART"].ToString();
            txtKFARBE.Text = dwTBABST["BMFARBE"].ToString();
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            try
            {
                DataRow row = SetData.ABPOS.NewRow();
                row["ABPMANDANT"] = "00";
                row["ABPAUFKRS"] = KRS;
                row["ABPAUFLFD"] = LFD;
                row["ABPAUFPOS"] = POS;
                Result = m_Data.BuildData(row);
                SetData.Clear();
                SetData.Merge(Result);
                if (vwABPOS != null)
                {
                    if (vwABPOS.View != null)
                    {
                        vwABPOS.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                SetData.Clear();
                SetData.Merge(Result);
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.DocType = "JLTAB45DOC";
            ITXEXRPT.strExcelFile = app.company + @"\JLTAB45DOC.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }


        private void btnPrintBCard_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.DocType = "JLTAB45DOC";
            ITXEXRPT.strExcelFile = app.company + @"\JLTAB45DOC.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void txtBCode_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                bool isFound = false;
                DataRow dwData = SetProd.BABST.NewRow();
                dwData["BMMANDANT"] = "00";
                dwData["BMBAUMKRS"] = Helpers.Helper.Left(txtBCode.Text, 2);
                dwData["BMBAUMLFD"] = Helpers.Helper.Right("000000" + txtBCode.Text.ToString().Substring(2).Trim(), 6);
                DataSet SResult = INTEXDBC.QueryBABST(dwData, 1);
                string BEWEGUNG = INTEXBL.GetAUFLFD("  ", "BEWEGUNG");
                DataRow dwBABEW = INTEXDBC.Convert2BABEW(null, app.company, false); 
                string _date="",_time="";
                Helpers.Helper.GetTime(ref _date, ref _time);
                foreach (DataRow dwBB in SResult.Tables[0].Rows)
                {
                    #region=====BABEW==20===========
                    dwBABEW["BBJJJJ"] = m_Data.year;
                    dwBABEW["BBBELEG"] = BEWEGUNG;
                    dwBABEW["BBLFDNR"] = "1";
                    dwBABEW["BBKART"] = dwBB["BMKART"];
                    dwBABEW["BBKVAR"] = dwBB["BMKVAR"];
                    dwBABEW["BBKFARBE"] = dwBB["BMFARBE"];
                    dwBABEW["BBZUSTAND"] = "    ";
                    dwBABEW["BBBAUMKRS"] = dwBB["BMBAUMKRS"];
                    dwBABEW["BBBAUMLFD"] = dwBB["BMBAUMLFD"];
                    dwBABEW["BBSPSTA"] = "  ";
                    dwBABEW["BBSPGRD"] = "  ";
                    dwBABEW["BBZENSUR"] = "1";
                    dwBABEW["BBTAX"] = "   ";
                    dwBABEW["BBLO"] =  dwBB["BMLO"];
                    dwBABEW["BBLB"] = dwBB["BMLB"];
                    dwBABEW["BBLPLATZ"] = dwBB["BMLP"];
                    dwBABEW["BBAUFBKZ"] = dwBB["BMAUFBKZ"];
                    dwBABEW["BBKOLLINR"] = dwBB["BMKOLLINR"];
                    dwBABEW["BBKETTTYP"] = dwBB["BMKETTTYP"];
                    dwBABEW["BBPARTIE"] = dwBB["BMPARTIE"];
                    dwBABEW["BBPARTIEKZ"] = "0";
                    dwBABEW["BBAUFTYP"] = "V";
                    dwBABEW["BBAUFKRS"] = dwBB["BMAUFKRS"];
                    dwBABEW["BBAUFLFD"] = dwBB["BMAUFLFD"];
                    dwBABEW["BBAUFPOS"] = dwBB["BMAUFPOS"];
                    dwBABEW["BBAFOLGE"] = "000";
                    dwBABEW["BBTEILNR"] = "0000";
                    dwBABEW["BBSA"] = "   ";
                    dwBABEW["BBSCHIENE"] = "     ";
                    dwBABEW["BBMNRKRS"] = "  ";
                    dwBABEW["BBMLFDNR"] = "0000";
                    dwBABEW["BBMPOS"] = "00";
                    dwBABEW["BBSCHICHT"] = "0";
                    dwBABEW["BBDL"] = " ";
                    dwBABEW["BBKSTKRS"] = "  ";
                    dwBABEW["BBKST"] = "0000";
                    dwBABEW["BBKOSTART"] = "      ";
                    dwBABEW["BBBS"] = "20";
                    dwBABEW["BBWERT"] = "0";
                    dwBABEW["BBWERTDAT"]= _date;
                    dwBABEW["BBWEKORR"] = "  ";
                    dwBABEW["BBMTR"] = dwBB["BMISTLG"];
                    dwBABEW["BBGEW"] = "0";
                    dwBABEW["BBMTRNOZU"] = "0";
                    dwBABEW["BBGEWNOZU"] = "0";
                    dwBABEW["BBBUCHDAT"] =_date; 
                    dwBABEW["BBBUCHZEIT"] = _time;
                    dwBABEW["BBBUCHBEN"] =app.User;
                    dwBABEW["BBFC"] = "BB03    ";
                    dwBABEW["BBMATABR"] = "J";
                    dwBABEW["BBINVBLG"] = "0";
                    dwBABEW["BBKONDAT"] = "0";
                    dwBABEW["BBSTORKZ"] = " ";
                    dwBABEW["BBDISPOKZ"] = " ";
                    dwBABEW["BBFILLER"] = "                                           ";
                    #endregion
                    INTEXDBC.SaveBABEW(dwBABEW,app.company);
                    #region=====BABEW=21============
                    dwBABEW["BBJJJJ"] = m_Data.year;
                    dwBABEW["BBBELEG"] = BEWEGUNG;
                    dwBABEW["BBLFDNR"] = "2";
                    dwBABEW["BBKART"] = dwBB["BMKART"];
                    dwBABEW["BBKVAR"] = dwBB["BMKVAR"];
                    dwBABEW["BBKFARBE"] = dwBB["BMFARBE"];
                    dwBABEW["BBZUSTAND"] = "    ";
                    dwBABEW["BBBAUMKRS"] = dwBB["BMBAUMKRS"];
                    dwBABEW["BBBAUMLFD"] = dwBB["BMBAUMLFD"];
                    dwBABEW["BBSPSTA"] = "  ";
                    dwBABEW["BBSPGRD"] = "  ";
                    dwBABEW["BBZENSUR"] = "1";
                    dwBABEW["BBTAX"] = "   ";
                    dwBABEW["BBLO"] = Helpers.Helper.Left(txtTLO.Text.PadRight(2), 2);
                    dwBABEW["BBLB"] = Helpers.Helper.Left(txtTLB.Text.PadRight(2), 2);
                    dwBABEW["BBLPLATZ"] = Helpers.Helper.Left(txtTLP.Text.PadRight(8), 8);
                    dwBABEW["BBAUFBKZ"] = dwBB["BMAUFBKZ"];
                    dwBABEW["BBKOLLINR"] = dwBB["BMKOLLINR"];
                    dwBABEW["BBKETTTYP"] = dwBB["BMKETTTYP"];
                    dwBABEW["BBPARTIE"] = dwBB["BMPARTIE"];
                    dwBABEW["BBPARTIEKZ"] = "0";
                    dwBABEW["BBAUFTYP"] = "V";
                    dwBABEW["BBAUFKRS"] = dwBB["BMAUFKRS"];
                    dwBABEW["BBAUFLFD"] = dwBB["BMAUFLFD"];
                    dwBABEW["BBAUFPOS"] = dwBB["BMAUFPOS"];
                    dwBABEW["BBAFOLGE"] = "000";
                    dwBABEW["BBTEILNR"] = "0000";
                    dwBABEW["BBSA"] = "   ";
                    dwBABEW["BBSCHIENE"] = "     ";
                    dwBABEW["BBMNRKRS"] = "  ";
                    dwBABEW["BBMLFDNR"] = "0000";
                    dwBABEW["BBMPOS"] = "00";
                    dwBABEW["BBSCHICHT"] = "0";
                    dwBABEW["BBDL"] = " ";
                    dwBABEW["BBKSTKRS"] = "  ";
                    dwBABEW["BBKST"] = "0000";
                    dwBABEW["BBKOSTART"] = "      ";
                    dwBABEW["BBBS"] = "21";
                    dwBABEW["BBWERT"] = "0";
                    dwBABEW["BBWERTDAT"] = _date;
                    dwBABEW["BBWEKORR"] = "  ";
                    dwBABEW["BBMTR"] = dwBB["BMISTLG"];
                    dwBABEW["BBGEW"] = "0";
                    dwBABEW["BBMTRNOZU"] = "0";
                    dwBABEW["BBGEWNOZU"] = "0";
                    dwBABEW["BBBUCHDAT"] = _date;
                    dwBABEW["BBBUCHZEIT"] = _time;
                    dwBABEW["BBBUCHBEN"] = app.User;
                    dwBABEW["BBFC"] = "BB03    ";
                    dwBABEW["BBMATABR"] = "J";
                    dwBABEW["BBINVBLG"] = "0";
                    dwBABEW["BBKONDAT"] = "0";
                    dwBABEW["BBSTORKZ"] = " ";
                    dwBABEW["BBDISPOKZ"] = " ";
                    dwBABEW["BBFILLER"] = "                                           ";
                    #endregion
                    INTEXDBC.SaveBABEW(dwBABEW, app.company);
                    dwBB["BMLO"] = Helpers.Helper.Left(txtTLO.Text.PadRight(2), 2);
                    dwBB["BMLB"] = Helpers.Helper.Left(txtTLB.Text.PadRight(2), 2);
                    dwBB["BMLP"] = Helpers.Helper.Left(txtTLP.Text.PadRight(8), 8);
                    dwBB["BMAENDBEN"] = app.User;
                    string date = "", year = "", month = "", day = "";
                    Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                    dwBB["BMAENDDAT"] = date;
                    isFound = true;
                    INTEXDBC.SaveBABST(dwBB, "JLT");
                  
                   


                }
                SetProd.Merge(SResult);
                if (vwTBABST.View != null)
                {
                    vwTBABST.View.MoveCurrentToFirst();
                }
                if (!isFound)
                {
                    MessageBox.Show("改轴没有找到");
                }
                txtBCode.Text = "";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            string filter = "BMKART LIKE '" + txtKART.Text.Trim() + "%' AND BMFARBE LIKE '" + txtKFARBE.Text.ToString().Trim() + "%'";
            cvTBABST.CustomFilter = filter;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            cvTBABST.CustomFilter = null;
        }
        private void txtABP_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }
    }
}
